import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Button } from 'semantic-ui-react'
import Styled from 'styled-components'
import { push } from 'react-router-redux'

const classNames = require('classnames')

export default props => <Button size="small" basic color="blue" {...props} />
export const BasicButton = props => <Button size="small" basic color="blue" {...props} />
export const PrimaryButton = ({ color, ...others }) => (
  <Button size="small" primary={color === undefined} color={color} {...others} />
)
const LinkButtonBasic = ({ goTo, src, ...otherProps }) => (
  <Button size="small" basic color="blue" onClick={() => goTo(src)} {...otherProps} />
)

const mapDispatchToProps = {
  goTo: path => push(path),
}

export const LinkButton = connect(
  null,
  mapDispatchToProps,
)(LinkButtonBasic)

export const SwitchButtonsStyle = Styled.div`
  display: inline-block;
  font-size: 14px;
  .checked {
    border-color: #1989fa!important;
    color: #1989fa;
  }
  .checked .inner:after {
    -webkit-transform: scale(1);
    transform: scale(1);
    opacity: 1;
    -webkit-transition: all .3s cubic-bezier(.78,.14,.15,.86);
    transition: all .3s cubic-bezier(.78,.14,.15,.86);
  }
  .border {
    border: 1px solid #dcdfe6;
    height: 40px;
    line-height: 40px;
    padding-left: 10px;
    padding-right: 10px;
    border-radius: 4px;
    transition: all .3s;
    font-size: 14px;
    margin-right: 8px;
    display: inline-block;
    cursor: pointer;
  }
  .border:hover {
    border-color: #1989fa!important;
    color: #1989fa;
  }
  .radio {
    vertical-align: middle;
    outline: none;
    line-height: 1;
    display: inline-block;
    position: relative;
    white-space: nowrap;
    cursor: pointer;
  }
  .check.radio {
    color: #1989fa!important;
  }
  .input {
    position: absolute;
    left: 0;
    z-index: 1;
    cursor: pointer;
    opacity: 0;
    top: 0;
    bottom: 0;
    right: 0;
    width: 14px;
    height: 14px;
  }
  .title {
    padding-left: 8px;
    padding-right: 8px;
  }
  .inner {
    position: relative;
    top: 0;
    left: 0;
    display: block;
    width: 14px;
    height: 14px;
    border-radius: 14px;
    border: 1px solid #dcdfe6;
    background-color: #f0f7ff;
    -webkit-transition: all .3s;
    transition: all .3s;
  }
  .inner:after {
    -webkit-transform: scale(1);
    transform: scale(1);
    opacity: 1;
    position: absolute;
    width: 6px;
    height: 6px;
    left: 3px;
    top: 3px;
    border-radius: 4px;
    display: table;
    border-top: 0;
    border-left: 0;
    content: " ";
    background-color: #1989fa;
    transform: scale(0);
    -webkit-transition: all .3s cubic-bezier(.78,.14,.15,.86);
    transition: all .3s cubic-bezier(.78,.14,.15,.86);
  }
`

export class SwitchButtons extends Component {
  state = { activeButton: undefined }

  componentDidMount() {
    const { ops } = this.props
    this.setState({ activeButton: ops[0].name })
  }

  render() {
    const { ops } = this.props
    const { activeButton } = this.state
    return (
      <SwitchButtonsStyle>
        {ops.map(button => (
          <label
            className={classNames({
              border: true,
              checked: button.name === activeButton,
            })}
            key={button.name}
            name={button.name}
            onClick={() => this.setState({ activeButton: button.name })}
          >
            <span className="radio">
              <input type="radio" className="input" />
              <span className="inner" />
            </span>
            <span className="title">{button.value}</span>
          </label>
        ))}
      </SwitchButtonsStyle>
    )
  }
}
